<?php
class UsersAction extends ZMAction {
      public function index(){
        $start = $_REQUEST['start'];
        $limit = $_REQUEST["limit"];
        $sort = $_REQUEST['sort'];
        $dir = $_REQUEST['dir'];
        if(!is_numeric($start)){
            $start = 0;
        }
        if(!is_numeric($limit)){
            $limit = 20;
        }
        $UserInfo = M("User");
        if($map){
            $count = $UserInfo->where($map)->count();
            if(empty($sort)){
                $result = $UserInfo->where($map)->limit("$start,$limit")->select();
            }else{
                $result = $UserInfo->where($map)->order("$sort $dir")->limit("$start,$limit")->select();
            }
        }else{
            $count = $UserInfo->count();
            if(empty($sort)){
                $result = $UserInfo->limit("$start,$limit")->select();
            }else{
                $result = $UserInfo->order("$sort $dir")->limit("$start,$limit")->select();
            }
        }
        foreach($result as &$row){
            $row['passwd'] = '';
        }
        $this->ajaxRetArray($result,$count);
      }
      
      public function del(){
        $user = $_SESSION['user'];
        if($user['isadmin']==1){
            $ids = $_REQUEST['ids'];
            if(!empty($ids)){
                $UserInfo = M("User");
                $UserInfo->where("userid<>1 and userid in ($ids)")->delete();
            }
            $this->success("删除成功！");
        }else{
            $this->error("只有管理员才能删除用户！");
        }
      }
      
      public function save(){
        $user = $_SESSION['user'];
        if($user['isadmin']<>1){
            $this->error("只有管理员才能修改用户！"); 
            return;
        }
        $UserInfo = M("User");
        $data = $UserInfo->create();
        $data['userid'] = $_REQUEST['userid'];
        $data['username'] = $_REQUEST['username'];
        if(!empty($_REQUEST['passwd'])){
            $data['passwd'] = md5($_REQUEST['passwd']);
        }
        if(!empty($_REQUEST['isadmin'])){
            $data['isadmin'] = 1;//!empty($_REQUEST['isadmin']);
        }else{
            $data['isadmin'] = 0;
        }
        if(!empty($data['userid'])){
            $UserInfo->save($data);
        }else{
            $UserInfo->add($data); 
        }
        $this->success("保存成功!");      
      }
}
